package com.mmall.service.impl;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.mmall.common.ResponseCode;
import com.mmall.common.ServerResponse;
import com.mmall.dao.CategoryMapper;
import com.mmall.pojo.Category;
import com.mmall.service.ICategroyService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;


@Service("iCategroyService ")
public class CategroyServiceImpl implements ICategroyService {


    //用来处理日志信息
    private org.slf4j.Logger logger = LoggerFactory.getLogger(CategroyServiceImpl.class);

    @Autowired
    private CategoryMapper categoryMapper;



    public ServerResponse addCategroy(String categroyName,Integer categroyId){

        //3.还需要判断一下id和商品名都有,这样才可以添加进去
        //id为空,或商品名称为空就不能添加
    if (categroyId == null || StringUtils.isBlank(categroyName)){
        return  ServerResponse.createByErrorMessage("添加商品类型的参数错误");
    }


    //判断用户名是否存在
        int selectAndcategoryAndName= categoryMapper.selectAndCategroyName(categroyName);
        if (selectAndcategoryAndName >0 ){
            return ServerResponse.createByErrorMessage("商品名已存在,请更换");
        }

        Category category = new Category();
        category.setId(categroyId);
        category.setName(categroyName);
        category.setStatus(true);

      int categrouCount = categoryMapper.insert(category);
      if (categrouCount>0){

          return  ServerResponse.createBySuccessMessage("商品添加成功");
      }
      return ServerResponse.createByErrorMessage("商品添加失败");
    }


    public ServerResponse updateCategroy(String NewcategroyName, Integer categroyId) {

        //3.还需要判断一下id和商品名都有,这样才可以添加进去
        //id为空,或商品名称为空就不能添加
        if (categroyId == null || StringUtils.isBlank(NewcategroyName)){
            return  ServerResponse.createByErrorMessage("修改商品类型的参数错误");
        }

        //判断用户名是否存在
        int selectAndcategoryAndName= categoryMapper.selectAndCategroyName(NewcategroyName);
        if (selectAndcategoryAndName >0 ){
            return ServerResponse.createByErrorMessage("商品名已存在,请更换");
        }

        //编辑新的商品信息
        Category category = new Category();
        category.setId(categroyId);
        category.setName(NewcategroyName);


        int categrouCount = categoryMapper.updateByPrimaryKeySelective(category);
        if (categrouCount>0){

            return  ServerResponse.createBySuccessMessage("商品更新成功");
        }
        return ServerResponse.createByErrorMessage("商品更新失败");

    }

    public ServerResponse<List<Category>> selectParentIdAndCategroy(Integer parentId){

        //查询他的子菜单
        List<Category> categoryList = categoryMapper.selectParentIdAndCategroy(parentId);

        //判断当前对象是否有值,集合是否为空
        if (CollectionUtils.isEmpty(categoryList)){
            logger.info("未找到当前分类的子分类");
        }
        return  ServerResponse.createBySuccess(categoryList);
    }



    //递归查找商品的根节点的子节点
    public ServerResponse<List<Integer>> selectRecursionAndParentIdAndCategroy(Integer parentId){

        //创建set集合
       Set<Category> setCategrou = Sets.newHashSet();
       //1.进行递归处理
        RecursionAndCategroy(setCategrou, parentId);

        //创建list集合
        ArrayList<Integer> listCategroy = Lists.newArrayList();

        //2.判断是否id是否存在,存在就set集合的id放到list里面
        if (parentId != null){
            //然后遍历子类的set
            for (Category categoryImte : setCategrou) {
                listCategroy.add(categoryImte.getId());
            }
        }
        return ServerResponse.createBySuccess(listCategroy);
    }


    //递归查询他的所有子节点集合
    private Set<Category> RecursionAndCategroy( Set<Category>  categroySet,Integer parentId){

        //获得对应的他下面的单个子分类信息
        Category category = categoryMapper.selectByPrimaryKey(parentId);

        //如果不为空,将子分类放到set集合里面去重
        if (category != null){
            categroySet.add(category);
        }

        //1.递归查询所以的根节点下的子节点
        List<Category> categoryList = categoryMapper.selectParentIdAndCategroy(parentId);
        //2.如果他的子节点没有,就会直接跳出,不会走递归操作
        for (Category categoryItem : categoryList) {
            //递归算法,把子节点放到对应的集合里面,并且set集合又去重的功能
            RecursionAndCategroy(categroySet,categoryItem.getId());
        }
        return  categroySet;
    }
}
